package com.shushan.accessibility;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.shushan.accessibility.coloros.ColorOSOperator;
import com.shushan.accessibility.emui.EmuiOperator;
import com.shushan.accessibility.flyme.FlymeOperator;
import com.shushan.accessibility.funtouch.FuntouchOperator;
import com.shushan.accessibility.miui.MiuiOperator;
import com.shushan.accessibility.samsung.SamsungOperator;
import com.shushan.accessibility.smartisan.SmartisanOperator;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.permission.PermissionAccessibilityActivity;
import com.shushan.util.ToastUtils;

/**
 * @ClassName: OpenAccessiSettingHelper
 * @Desciption: 辅助功能帮助类
 * @author: yichaohua
 * @date: 2018-07-02
 */
public class OpenAccessiSettingHelper {

    private static final String TAG = "OpenAccessiSettingHelper";

    private static Handler mHandler = new Handler(Looper.getMainLooper());

    public static void jumpToSettingPage(BaseActivity context) {
        context.setAllowBack(true);
        try {
            Intent intent = new Intent(context,  AccessibilityOpenActivity.class);
            intent.putExtra("action", Constants.ACTION_START_ACCESSIBILITY_SETTING);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            context.setAllowBack(false);
            e.printStackTrace();
        }
    }

    public static void jumpToSettingPage(Context context) {
        try {
            Intent intent = new Intent(context,  AccessibilityOpenActivity.class);
            intent.putExtra("action", Constants.ACTION_START_ACCESSIBILITY_SETTING);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeSavePower(final Context context) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                RomType romType = RomUtils.getRomType(context);
                boolean result = false;
                switch (romType){
                    case FUNTOUCHOS_ROM:
                        result = FuntouchOperator.getInstance().closeSavePower();
                        break;
                    case EMUI_ROM:
                        result = EmuiOperator.getInstance().closeSavePower(context);
                        break;
                    case MIUI_ROM:
                        result = MiuiOperator.getInstance().closeSavePower();
                        break;
                    case COLOROS_ROM:
                        result = ColorOSOperator.getInstance().closeSavePower();
                        break;
                    case SAMSUNG_ROM:
                        result = SamsungOperator.getInstance().closeSavePower();
                        break;
                    case SMARTISAN_ROM:
                        result = SmartisanOperator.getInstance().closeSavePower();
                        break;
                    default:
                        break;
                }

                onBackAction(context,result,500);

            }
        }, 2000);

    }

    public static void openComplete(final Context context) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                RomType romType = RomUtils.getRomType(context);
                boolean result = false;
                switch (romType){
                    case FUNTOUCHOS_ROM:
                        break;
                    case EMUI_ROM:
                        result = EmuiOperator.getInstance().openComplete(context);
                        break;
                    case MIUI_ROM:
                        result = MiuiOperator.getInstance().openComplete();
                        break;
                    case COLOROS_ROM:
                        result = ColorOSOperator.getInstance().openComplete();
                        break;
                    case SAMSUNG_ROM:
                        result = SamsungOperator.getInstance().openComplete();
                        break;
                    case SMARTISAN_ROM:
                        result = SmartisanOperator.getInstance().openComplete();
                        break;
                    case FLYME_ROM:
                        result = FlymeOperator.getInstance().openComplete();
                        break;
                    default:
                        break;
                }

                onBackAction(context,result,200);

            }
        }, 2000);

    }

    public static void openFloatManager(final Context context) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                RomType romType = RomUtils.getRomType(context);
                boolean result = false;
                switch (romType){
                    case FUNTOUCHOS_ROM:
                        break;
                    case EMUI_ROM:
                        result = EmuiOperator.getInstance().openFloatManager();
                        break;
                    case MIUI_ROM:
                        result = MiuiOperator.getInstance().openFloatManager();
                        break;
                    case COLOROS_ROM:
                        result = ColorOSOperator.getInstance().openFloatManager();
                        break;
                    case SAMSUNG_ROM:
                        result = SamsungOperator.getInstance().openFloatManager();
                        break;
                    case SMARTISAN_ROM:
                        result = SmartisanOperator.getInstance().openFloatManager();
                        break;
                    case FLYME_ROM:
                        result = FlymeOperator.getInstance().openFloatManager();
                        break;
                    default:
                        break;
                }

                onBackAction(context,result,300);

            }
        }, 2000);
    }

    public static void openUsageSetting(final Context context) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                RomType romType = RomUtils.getRomType(context);
                boolean result = false;
                switch (romType){
                    case FUNTOUCHOS_ROM:
                        result = FuntouchOperator.getInstance().openUsageSetting();
                        break;
                    case EMUI_ROM:
                        result = EmuiOperator.getInstance().openUsageSetting();
                        break;
                    case MIUI_ROM:
                        result = MiuiOperator.getInstance().openUsageSetting();
                        break;
                    case COLOROS_ROM:
                        result = ColorOSOperator.getInstance().openUsageSetting();
                        break;
                    case SAMSUNG_ROM:
                        result = SamsungOperator.getInstance().openUsageSetting();
                        break;
                    case SMARTISAN_ROM:
                        result = SmartisanOperator.getInstance().openUsageSetting();
                        break;
                    case FLYME_ROM:
                        result = FlymeOperator.getInstance().openUsageSetting();
                        break;
                    default:
                        break;
                }

                onBackAction(context,result,600);

            }
        }, 2000);
    }


    public static void enableAdmin(final Context context) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                RomType romType = RomUtils.getRomType(context);
                boolean result = false;
                switch (romType){
                    case FUNTOUCHOS_ROM:
                        result = FuntouchOperator.getInstance().enableAdmin();
                        break;
                    case EMUI_ROM:
                        result = EmuiOperator.getInstance().enableAdmin(context);
                        break;
                    case MIUI_ROM:
                        result = MiuiOperator.getInstance().enableAdmin();
                        break;
                    case COLOROS_ROM:
                        result = ColorOSOperator.getInstance().enableAdmin();
                        break;
                    case SAMSUNG_ROM:
                        result = SamsungOperator.getInstance().enableAdmin();
                        break;
                    case SMARTISAN_ROM:
                        result = SmartisanOperator.getInstance().enableAdmin();
                        break;
                    case FLYME_ROM:
                        result = FlymeOperator.getInstance().enableAdmin();
                        break;
                    default:
                        break;
                }

                onBackAction(context,result,400);

            }
        }, 2000);
    }

    public static void onBackAction(Context context,boolean success,int resultCode){
        if(!success){
            switch (resultCode){
                case 200:
                    ToastUtils.showToast(context,"自动开启自启动失败，请尝试手动开启");
                    break;
                case 300:
                    ToastUtils.showToast(context,"自动开启悬浮窗权限失败，请尝试手动开启");
                    break;
                case 400:
                    ToastUtils.showToast(context,"自动激活设备管理器失败，请尝试手动开启");
                    break;
                case 500:
                    ToastUtils.showToast(context,"自动关闭省电模式失败，请尝试手动关闭");
                    break;
                case 600:
                    ToastUtils.showToast(context,"自动打开查看应用使用情况失败，请尝试手动开启");
                    break;
                default:
                    break;
            }

        }

        Intent intent = new Intent();
        intent.putExtra("result", success);
        intent.putExtra("resultCode", resultCode);
        intent.putExtra(Constants.ACTION, Constants.ACTION_FINISH_SELF);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(context, PermissionOpenActivity.class);
        context.startActivity(intent);
    }
}
